package com.thememaker.util.packager;

import com.thememake.common.base.Strings;
import com.thememaker.lockstyle.bean.CheckOut;
import com.thememaker.lockstyle.bean.Lockscreen;
import com.thememaker.lockstyle.bean.command.VariableCommand;
import com.thememaker.lockstyle.bean.ui.Image;
import com.thememaker.lockstyle.bean.ui.Slider;
import com.thememaker.lockstyle.control.LayoutComponentUtils;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public class OS360CodeCheck implements CodeCheck {

    @Override
    public void checkLockscreen(Lockscreen lockscreen, Consumer<CheckOut> callBack) {

        if(!Strings.isNullOrEmpty(lockscreen.getShowNotification())){
            callBack.accept(new CheckOut(1,Error_Lockscreen_Notification_Code + Error_Code + lockscreen.toString()));
        }
        if (Strings.isNullOrEmpty(lockscreen.getScreenWidth()) || !lockscreen.getScreenWidth().equals("1080")){
            callBack.accept(new CheckOut(1,Error_Lockscreen_ScreenWidth + Error_Code + lockscreen.toString()));
        }
        if (Strings.isNullOrEmpty(lockscreen.getScreenWidth())){
            callBack.accept(new CheckOut(1,Error_Lockscreen_Vibrate + Error_Code + lockscreen.toString()));
        }
    }

    @Override
    public void checkImage(Image image, Consumer<CheckOut> callBack) {
        if (!Strings.isNullOrEmpty(image.getSrc()) && !Strings.isNullOrEmpty(image.getSrcid())){
            if (image.getSrc().equals(TimeSrcCode)){
                if (image.getSrcid().contains("#hour24/10")){
                    if (!image.getSrcid().trim().equals(Hour1SrcIdCode) ){
                        callBack.accept(new CheckOut(1,Error_Time_Hour12 + Error_Code + image.toString()));
                    }
                }
                if (image.getSrcid().contains("#hour24%10")){
                    if (!image.getSrcid().trim().equals(Hour2SrcIdCode) ){
                        callBack.accept(new CheckOut(1,Error_Time_Hour12 + Error_Code + image.toString()));
                    }
                }
            }else if ((image.getRotationAnimation()!=null)){
                if(LayoutComponentUtils.engine==1){
                    if (image.getPointX()==null || image.getPointY()==null){
                        callBack.accept(new CheckOut(1,Error_rotation_center + Error_Code + image.toString()));
//                callBack.accept(Error_rotation_center + Error_Code + image.toString());
                        System.out.println("走了这一步-------------");
                    }
                }

            }
        }
    }

    @Override
    public void checkSlider(Slider slider, Consumer<CheckOut> callBack) {
        checkSliderInitSpeed(slider,callBack);
        if (slider.getStartPoint() != null){
            if (slider.getStartPoint().getReachedUpState() != null || slider.getStartPoint().getReachedDownState() != null){
                callBack.accept(new CheckOut(1,Error_Slider_ReachedUpState + Error_Code + slider.toString()));
            }
           Optional.ofNullable(slider.getStartPoint())
                    .flatMap(startPoint -> Optional.ofNullable(startPoint.getReachedState()))
                    .flatMap(reachedStateOptional -> Optional.ofNullable(reachedStateOptional.getTrigger()))
                    .flatMap(trigger -> Optional.ofNullable(trigger.getCommandMap()))
                   .ifPresent(commands -> commands.keySet().forEach(key -> {
                       if (commands.get(key) instanceof VariableCommand) {
                           VariableCommand variableCommand = (VariableCommand) commands.get(key);
                           if (Strings.isNullOrEmpty(variableCommand.getConstString())){
                               callBack.accept(new CheckOut(1,Error_Var_Const+Error_Code+variableCommand.toString()));
                           }
                       }
                   }));
        }
    }

    @Override
    public void showErrorNotification(Boolean canShowError, Consumer<CheckOut> callBack) {
        if (!canShowError){
            callBack.accept(new CheckOut(1,Error_More_Notification));
        }
    }

}
